package com.mdnote.practice.binary_tree;

/**
 * @author Rhythm-2019
 * @date 2020/10/4
 * @description  合并二叉树
 */
public class LeetCode617 {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {

        return _build(t1, t2);

    }
    // recursion
    public TreeNode _build(TreeNode t1, TreeNode t2) {
        // Terminator
        if (t1 == null && t2 == null) {
            return null;
        }
        // Process
        // 想办法构建自己
        int val = (t1 != null ? t1.val : 0) + (t2 != null ? t2.val : 0);
        TreeNode root = new TreeNode(val);

        // Drill down
        root.left = _build(t1 == null ? null :t1.left, t2 == null ? null : t2.left);
        root.right = _build(t1 == null? null :t1.right, t2 == null ? null : t2.right);

        return root;
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
